extends Control

signal talkOver

# @export_file('*.json') var words
@onready var chatBox=$NinePatchRect
@onready var person=$NinePatchRect/Name
@onready var text=$NinePatchRect/Text

var talks=[]
var cur=-1
# 对话框激活状态
var active:=false:
    set(v):
        chatBox.visible=v
        active=v
        if !active: emit_signal('talkOver') 

func _ready() -> void:
    active=false

func _input(event: InputEvent) -> void:
    if !active:return
    if event.is_action_pressed("ui_accept"):nextWord()

func start():
    cur=-1
    active=true
    talks=loadJson()
    nextWord()

func loadJson():
    var file=FileAccess.open('res://人物/台词.json',FileAccess.READ)
    var content=JSON.parse_string(file.get_as_text())
    return content

func nextWord():
    cur+=1
    if cur>=len(talks):active=false;return

    person.text=talks[cur].name
    text.text=talks[cur].word